package lmh.example.exam3_11;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;


import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    public static final String TAG = MainActivity.class.getName();
    private ProgressBar download_progress,post_progress;
    private TextView download_text,post_text;
    public static String basePath = Environment.getExternalStorageDirectory().getAbsolutePath();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        download_progress = (ProgressBar) findViewById(R.id.download_progress);
        download_text = (TextView) findViewById(R.id.download_text);

        post_progress = (ProgressBar) findViewById(R.id.post_progress);
        post_text = (TextView) findViewById(R.id.post_text);

        findViewById(R.id.ok_download).setOnClickListener(this);
        findViewById(R.id.ok_post_file).setOnClickListener(this);

        int permission = ActivityCompat.checkSelfPermission(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // 请求读写存储器权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}, 10);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ok_download:  //  单击“下载”按钮
                String url = "http://10.0.2.2:8089/Server_war_exploded/download/test.gif";
                final String fileName = url.split("/")[url.split("/").length - 1];
                Log.i(TAG, "fileName==" + fileName);
                OkHttpUtil.downloadFile(url, new ProgressListener() {
                    @Override
                    public void onProgress(long currentBytes, long contentLength, boolean done) {
                        Log.i(TAG, "currentBytes==" + currentBytes + "==contentLength==" + contentLength + "==done==" + done);
                        int progress = (int) (currentBytes * 100 / contentLength);
                        download_progress.setProgress(progress);
                        download_text.setText(progress + "%");
                    }
                }, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (response != null) {
                            InputStream is = response.body().byteStream();
                            // 存储到手机
                            FileOutputStream fos = new FileOutputStream(new File(basePath + "/" + fileName));
                            int len = 0;
                            byte[] buffer = new byte[2048];
                            while (-1 != (len = is.read(buffer))) {
                                fos.write(buffer, 0, len);
                            }
                            fos.flush();
                            fos.close();
                            is.close();
                        }
                    }
                });
                break;
            case R.id.ok_post_file:
                File file = new File(basePath + "/big_buck.mp4");
                String postUrl = "http://10.0.2.2:8089/Server_war_exploded/UploadFileServlet";

                OkHttpUtil.postFile(postUrl, new ProgressListener() {
                    @Override
                    public void onProgress(long currentBytes, long contentLength, boolean done) {
                        Log.i(TAG, "currentBytes==" + currentBytes + "==contentLength==" + contentLength + "==done==" + done);
                        int progress = (int) (currentBytes * 100 / contentLength);
                        post_progress.setProgress(progress);
                        post_text.setText(progress + "%");
                    }
                }, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (response != null) {
                            String result = response.body().string();
                            Log.i(TAG, "result===" + result);
                        }
                    }
                }, file);

                break;
        }
    }
}
